<?php

namespace api\modules\v1\controllers\rbac;

use Yii;
use api\controllers\OnAuthController;
use api\modules\v1\forms\rbac\ModuleForm;
use common\models\member\Member;
use common\models\rbac\Module;
use common\helpers\CommonHelper;

/**
 * Class ModuleController
 * @package api\modules\v1\controllers\rbac
 */
class ModuleController extends OnAuthController
{
    /**
     * @var Module
     */
    public $modelClass = Module::class;

    /**
     * @api {get} /v1/rbac/module/list 模块列表
     * @apiName list
     * @apiGroup Module
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   部门列表
     * @apiSuccess {Number}  data.list.id                模块ID
     * @apiSuccess {String}  data.list.name              模块名称
     * @apiSuccess {String}  data.list.flag              模块标识
     * @apiSuccess {String}  data.list.url               模块链接url
     * @apiSuccess {String}  data.list.class_style       模块ico
     * @apiSuccess {Number}  data.list.sort              排序
     * @apiSuccess {Number}  data.list.status            状态
     * @apiSuccess {String}  data.list.status_str        状态值
     * @apiSuccess {String}  data.list.created_at_str    创建时间
     */
    public function actionList()
    {
        if (Yii::$app->user->identity->member->is_super != Member::IS_SUPER_YES) {
            return CommonHelper::api(422, '非超管，无权限访问');
        }

        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $data = Yii::$app->services->module->getListPage($params, (int)$page, (int)$pageSize);

        $data['list'] = Yii::$app->services->module->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/rbac/module/info 菜单详情
     * @apiName info
     * @apiGroup Module
     *
     * @apiDescription 新增ID不传，编辑必传ID
     *
     * @apiParam {Number} id 菜单ID【选填】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 菜单ID
     * @apiSuccess {Number} data.parent_menu_id     父菜单ID
     * @apiSuccess {Number} data.module_id          模块ID
     * @apiSuccess {String} data.name               菜单名称
     * @apiSuccess {String} data.url                菜单Url
     * @apiSuccess {String} data.class_style        菜单icon
     * @apiSuccess {Number} data.status             菜单状态
     * @apiSuccess {Number} data.sort               菜单排序
     */
    public function actionInfo()
    {
        $id = (int)Yii::$app->request->get('id', 0);

        // 菜单信息
        $module = Yii::$app->services->module->findById($id);

        return [
            'id' => $module->id ?? 0,
            'name' => $module->name ?? '',
            'url' => $module->url ?? '',
            'class_style' => $module->class_style ?? '',
            'status' => $module->status ?? Module::STATUS_ENABLE,
            'sort' => $module->sort ?? 0,
        ];
    }

    /**
     * @api {post} /v1/rbac/module/create 新增模块
     * @apiName create
     * @apiGroup Module
     *
     * @apiParam {String} name              模块名称
     * @apiParam {String} url               模块url
     * @apiParam {String} class_style       模块ico
     * @apiParam {String} sort              排序徐
     * @apiParam {Number} status            状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         模块ID
     */
    public function actionCreateModule()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new ModuleForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->module->create($model);
    }

    /**
     * @api {post} /v1/rbac/module/update 编辑模块
     * @apiName update
     * @apiGroup Module
     *
     * @apiParam {Number} id                模块ID
     * @apiParam {String} name              模块名称
     * @apiParam {String} url               模块url
     * @apiParam {String} class_style       模块ico
     * @apiParam {String} sort              排序徐
     * @apiParam {Number} status            状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         模块ID
     */
    public function actionUpdateModule()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new ModuleForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->module->edit($model);
    }


    /**
     * @api {post} /v1/rbac/module/delete 删除模块
     * @apiName delete
     * @apiGroup Module
     *
     * @apiParam {Number} id                模块ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         模块ID
     */
    public function actionDeleteModule()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;

        $model = new ModuleForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->module->delete($model);
    }

}
